subscribed.mediator
-
Declaration
structMediator(params...);A simple mediator implementation. More precisely, an event collection with a unified interface and beforeEach/afterEach hooks.
Parameters
Examples
The mediator events can be strings.
Mediator!( "start", void delegate(), "stop", void delegate() ) mediator; mediator.emit!"start"();
Examples
The mediator events can be enum members.
enum Event { start, stop } Mediator!( Event.start, void delegate(), Event.stop, void delegate() ) mediator; mediator.emit!(Event.start)();
Examples
The mediator events cannot be of mixed type.
immutable canCompile = __traits(compiles, Mediator!( "start", void delegate(), 3, void delegate() )); assert(!canCompile, "Can compile mediators with mixed index types");
Examples
The mediator can subscribe, unsubscribe and broadcast events.
Mediator!( "inc", void delegate(), "dec", void delegate(), "reset counter", void delegate() ) mediator; int counter; void increment() { counter++; } void decrement() { counter--; } void reset() { counter = 0; } mediator.on!"inc"(&increment); mediator.on!"dec"(&decrement); mediator.on!"reset counter"(&reset); assert(counter == 0, "Mediator functions are called before any action is performed"); mediator.emit!"inc"(); assert(counter == 1, "The mediator does not call one of it's functions"); mediator.emit!"dec"(); assert(counter == 0, "The mediator does not call one of it's functions"); assert(counter == 0, "Mediator functions are called before any action is performed"); mediator.emit!"inc"(); assert(counter == 1, "The mediator does not call one of it's functions"); mediator.emit!"reset counter"(); assert(counter == 0, "The mediator does not call one of it's functions"); mediator.beforeEach ~= string => false; assert(counter == 0, "The beforeEach hook does not work"); mediator.emit!"inc"(); assert(counter == 0, "The beforeEach hook does not work"); mediator.emit!"dec"(); assert(counter == 0, "The beforeEach hook does not work"); mediator.beforeEach.clear(); mediator.off!"inc"(&increment); mediator.off!"dec"(&decrement); assert(counter == 0, "The mediator called one of it's functions while unregistering them"); mediator.emit!"inc"(); assert(counter == 0, "The mediator did not remove a listener"); mediator.emit!"dec"(); assert(counter == 0, "The mediator did not remove a listener");
-
Declaration
aliasIType= typeof(params[0]);The type of the (indexing) channel names.
-
Declaration
Event!(bool delegate(IType))beforeEach;The hook to be executed before any transition. If
falseis returned, the no transition occurs. -
Declaration
Event!(void delegate(IType))afterEach;The hook to be executed after a successful transition.
-
Declaration
voidon(IType channel)(EventType.ListenerType[]listeners...);A function for appending
listenersto the channel event.Parameters
channelThe channel whose event to subscribe to.
EventType.ListenerType[]listenersThe
listenersto append.See Also
subscribed.event.Event.append
-
Declaration
voidoff(IType channel)(EventType.ListenerType[]listeners...);A function for removing
listenersfrom the channel event.Parameters
channelThe channel whose event to remove from.
EventType.ListenerType[]listenersThe
listenersto remove.See Also
subscribed.event.Event.append
-
Declaration
voidemit(string channel)(EventType.ParamTypesparams);Calls all the registered listeners for the channel in order.
Parameters
channelThe channel to
emita message to.EventType.ParamTypesparamsThe param tuple to call the listeners with.
Return Value
An array of results from the listeners. If
EventType.ReturnTypeis void, then this function also returns void.See Also
subscribed.event.Event.call